package com.patsnap.data.npd.dw.etl.utils;

import com.amazonaws.SDKGlobalConfiguration;
import com.amazonaws.auth.*;
import com.patsnap.data.common.aws.AWSCredentialsProviderUtils;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.io.InputStream;
import java.util.List;

/**
 * create credential for aws service with keys
 *
 * @author lwying
 */
public class CredentialsUtils {
    public final static String AWS_DATATEAM_CREDENTIALS = "/.aws-datateam/credentials";

    public final static String AWS_WEBSITE_CREDENTIALS = "/.aws-website/credentials";

    public static final String PROD_DB_PRROXY =
            "http://s-platform-dynamodb-proxy.patsnap.private/747875099153/us-east-1";

    /**
     * load credentials
     *
     * @param filepath
     * @throws Exception
     */
    @SuppressWarnings("deprecation")
    public static AWSCredentials getCredentialsFromFile(String filepath) throws Exception {
        List<String> lines = FileUtils.readLines(new File(filepath));
        return getCredentials(lines);
    }

    public static AWSCredentials getCredentials(List<String> lines) {
        String acckey = null;
        String seckey = null;
        String token = null;
        for (String string : lines) {
            if (StringUtils.isNotEmpty(string) && !StringUtils.startsWith(string, "#")) {
                string = string.trim();
                String[] ss = string.split("=");
                if (ss != null && ss.length > 1) {
                    if (StringUtils.equalsIgnoreCase(ss[0], SDKGlobalConfiguration.ACCESS_KEY_SYSTEM_PROPERTY)) {
                        acckey = String.valueOf(ss[1]);
                    }
                    //
                    if (StringUtils.equalsIgnoreCase(ss[0], SDKGlobalConfiguration.SECRET_KEY_SYSTEM_PROPERTY)) {
                        seckey = String.valueOf(ss[1]);
                    }
                    if (StringUtils.equalsIgnoreCase(ss[0], SDKGlobalConfiguration.AWS_SESSION_TOKEN_ENV_VAR)) {
                        token = String.valueOf(ss[1]);
                    }
                }
            }
        }
        //create credentials
        if (StringUtils.isNotEmpty(token)) {
            return getCredentials(acckey, seckey, token);
        }
        return getCredentials(acckey, seckey);
    }

    /**
     * load credentials
     *
     * @throws Exception
     */
    @SuppressWarnings("deprecation")
    public static AWSCredentials getCredentialsFromStream(InputStream is) throws Exception {
        List<String> lines = null;
        try {
            lines = IOUtils.readLines(is);
        } catch (Exception e) {
            is.close();
        }
        return getCredentials(lines);
    }

    /**
     * get credential with aws key
     *
     * @return
     * @throws Exception
     */
    public static AWSCredentials getCredentials() throws Exception {
        DefaultAWSCredentialsProviderChain chain = new DefaultAWSCredentialsProviderChain();
        AWSCredentials credentials = chain.getCredentials();
        return credentials;
    }

    /**
     * get credential
     *
     * @param accessKey
     * @param secretKey
     * @return
     */
    public static AWSCredentials getCredentials(String accessKey, String secretKey) {
        AWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey);
        return credentials;
    }

    /**
     * get credential with session token
     *
     * @param accessKey
     * @param secretKey
     * @param token
     * @return
     */
    public static AWSCredentials getCredentials(String accessKey, String secretKey, String token) {
        AWSCredentials credentials = new BasicSessionCredentials(accessKey, secretKey, token);
        return credentials;
    }

    /**
     * get datateam credential for docker
     * replaced by {@link CredentialsUtils#getDockerDatateamCredentialsProvider()} ()}
     *
     * @return
     */
    @Deprecated
    public static AWSCredentials getDockerDatateamCredentials() throws Exception {
        return getCredentialsFromFile(AWS_DATATEAM_CREDENTIALS);
    }

    /**
     * get website credential for docker
     * replaced by {@link CredentialsUtils#getDockerWebsiteCredentialsProvider()}
     *
     * @return
     */
    @Deprecated
    public static AWSCredentials getDockerWebsiteCredentials() throws Exception {
        return getCredentialsFromFile(AWS_WEBSITE_CREDENTIALS);
    }

    public static final String STS_ROLE_DATATEAM = "arn:aws:iam::006694404643:role/sts-data-self-cross-role";

    public static final String STS_ROLE_WEBSITE = "arn:aws:iam::747875099153:role/DataCrossAccountRole";

    /**
     * get datateam credential for docker
     *
     * @return
     */
    public static AWSCredentialsProvider getDockerDatateamCredentialsProvider() {
        return AWSCredentialsProviderUtils.getSTSProvider(null, STS_ROLE_DATATEAM, null);
    }

    /**
     * get website credential for docker
     *
     * @return
     */
    public static AWSCredentialsProvider getDockerWebsiteCredentialsProvider() {
        return AWSCredentialsProviderUtils.getSTSProvider(null, STS_ROLE_WEBSITE, null);
    }

}
